package xj.graph2d.app.prop;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Line2D;

import javax.swing.Icon;

import xj.graph2d.DrawAttr;
import xj.graph2d.StrokeMap;

public class LineWidthIcon implements Icon {

  protected Dimension dim = null;

  protected int width = 1;

  protected int style = DrawAttr.SOLID_LINE;

  public LineWidthIcon() {
    this(32, 16);
  }

  public LineWidthIcon(int w, int h) {
    dim = new Dimension(w, h);
  }

  public void setWidth(int width) {
    if (width > dim.height) {
      width = dim.height;
    }
    this.width = width;
  }

  public void setStyle(int style) {
    this.style = style;
  }

  public int getWidth() {
    return width;
  }

  public int getStyle() {
    return style;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g;
    Color oldColor = g.getColor();
    Stroke oldStroke = g2.getStroke();
    //
    // g2.setColor(Color.white);
    // g2.fillRect(x, y, getIconWidth(), getIconHeight());
    //
    //g2.setColor(Color.black);
    g2.setColor(DrawAttr.defaultPenColor);
    StrokeMap smap = StrokeMap.getInstance();
    Stroke stk = smap.findStroke(width, DrawAttr.LINE_CAP_BUTT,
				 DrawAttr.LINE_JOIN_MITER, style);
    g2.setStroke(stk);
    float yy = y + dim.height / 2f;
    g2.draw(new Line2D.Float(x, yy, x + dim.width, yy));
    g2.setStroke(oldStroke);
    g2.setColor(oldColor);
  }

  public int getIconWidth() {
    return dim.width;
  }

  public int getIconHeight() {
    return dim.height;
  }

}
